// setup-icons.js - 图标生成脚本（在浏览器中运行）
// 在浏览器控制台中运行此代码，或将其作为 HTML 页面的一部分

(function() {
  'use strict';

  // 使用固定种子确保每次生成的 logo 相同
  const SEED = 12345;
  
  function seededRandom(seed) {
    let value = seed;
    return function() {
      value = (value * 9301 + 49297) % 233280;
      return value / 233280;
    };
  }

  const random = seededRandom(SEED);

  function generateColor() {
    const hue = Math.floor(random() * 360);
    const saturation = 60 + Math.floor(random() * 30);
    const lightness = 40 + Math.floor(random() * 20);
    return `hsl(${hue}, ${saturation}%, ${lightness}%)`;
  }

  function generateIcon(size) {
    const canvas = document.createElement('canvas');
    canvas.width = size;
    canvas.height = size;
    const ctx = canvas.getContext('2d');

    // 背景渐变
    const gradient = ctx.createLinearGradient(0, 0, size, size);
    const color1 = generateColor();
    const color2 = generateColor();
    gradient.addColorStop(0, color1);
    gradient.addColorStop(1, color2);
    ctx.fillStyle = gradient;
    ctx.fillRect(0, 0, size, size);

    // 绘制音乐符号
    ctx.strokeStyle = '#ffffff';
    ctx.fillStyle = '#ffffff';
    ctx.lineWidth = size / 20;
    ctx.lineCap = 'round';
    ctx.lineJoin = 'round';

    const centerX = size / 2;
    const centerY = size / 2;
    const radius = size / 4;

    // 圆形（音波）
    ctx.beginPath();
    ctx.arc(centerX, centerY, radius * 0.6, 0, Math.PI * 2);
    ctx.stroke();

    // 音符杆
    ctx.beginPath();
    ctx.moveTo(centerX + radius * 0.6, centerY - radius * 0.3);
    ctx.lineTo(centerX + radius * 0.6, centerY - radius * 1.2);
    ctx.stroke();

    // 音符头部
    ctx.beginPath();
    ctx.arc(centerX + radius * 0.6, centerY - radius * 1.2, radius * 0.2, 0, Math.PI * 2);
    ctx.fill();

    // 装饰线条
    ctx.beginPath();
    ctx.moveTo(centerX - radius * 0.8, centerY + radius * 0.4);
    ctx.lineTo(centerX + radius * 0.8, centerY + radius * 0.4);
    ctx.stroke();

    return canvas;
  }

  function downloadIcon(canvas, filename) {
    canvas.toBlob((blob) => {
      const url = URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = filename;
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
      URL.revokeObjectURL(url);
    }, 'image/png');
  }

  // 生成并下载所有图标
  function generateAllIcons() {
    const sizes = [16, 48, 128];
    sizes.forEach((size, index) => {
      setTimeout(() => {
        const canvas = generateIcon(size);
        downloadIcon(canvas, `icon${size}.png`);
      }, index * 500);
    });
  }

  // 导出到全局
  if (typeof window !== 'undefined') {
    window.generateExtensionIcons = generateAllIcons;
  }

  // 如果直接运行，自动生成
  if (typeof document !== 'undefined' && document.body) {
    generateAllIcons();
  }
})();

